//
//  UIBarButtonItem + Extension.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    /// 自定义导航按钮
    ///
    /// - parameter imageName:   默认图片
    /// - parameter bgImageName: 背景图片
    ///
    /// - returns: 导航按钮
    convenience init(imageName: String ,bgImageName:String,target : Any? , action : Selector ) {
        //初始化按钮
         let btn = UIButton(type: .custom)
        //先获得图片
        let nonImage = UIImage(named: imageName)
        //获得图片尺寸
        let imageSize = nonImage?.size ?? CGSize()
        let rect = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
        btn.frame = rect
        //设置默认图片
        btn.setImage(UIImage(named: imageName), for: .normal)
        //设置高亮图片
        btn.setBackgroundImage(UIImage(named:bgImageName), for: .highlighted)
        //添加按钮到初始化方法
        
        //点击事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        self.init(customView: btn)
    }
}
